/* following 3 imports added for IP fetch routine */

import oscP5.*;
import netP5.*;
import java.io.*;
import java.util.*;

//! OSC 
NetAddress Conductor; //for sending OSC messages to the ipad
OscP5 oscP5;
OscP5 oscP5x;
/* map OSC controllers to user variables for sketches */


String pmsg = ""; // previous OSC message
String cmsg = ""; // current OSC Message
String fmsg = ""; // nexpected OSC Message
String statMsg="";
String inputMsg="";

int knownFacet=16;
int goFacet=16;
int transMode=1;

float oscX, oscY = 0.0;
float oscRot;

//! controller limits and locks
boolean rotLoc=true;
boolean rotLim=true;

//! setup arrays for state control
String IP;
String MAC;

int[] flowers = new int[16]; 
int[] facets = new int[16];
float[] ranges = new float[8];
String[] facetName = new String[17];

/* values from bloom.i file, or make 'em up! */
/* !! need error control !! */

int screenX;
int screenY;

//! functional constants
float gm = 1.60;
float gr = 0.60;

PFont font;

//! init methods

boolean KeyAct=false;
boolean OscAct=true;
boolean FacetAct=false;
boolean Activity=false;
boolean SoundAct=false;
boolean DisplayAct=false;
boolean sett = false;
boolean rd = false;
boolean facetColor = false;

void draw() {
  //  fill(9,9);
  if (!sett) {
    onetimeSet();
    sett=true;
  }
  if (rd) {
    System.gc();
    redraw();
    rd=false;
  }
  if ((KeyAct)||(OscAct)||(FacetAct)) { 
    Activity=true;
  } else {
    if ((!androidMode)&&((DisplayAct)||(SoundAct))) {
      Activity=true;
    } else Activity=false;
  }
  if (Activity) {
    commonStage();
  }
  //  KeyAct=false;
  //  OscAct=false;
}


void stageSBA() {//showBloom Address
  background(0);
  colorMode(RGB, 100);
  fill(100);
  textSize(18);
  textAlign(CENTER);
  text(("bloom "+bloom[1]), width/2, height/2);
}

public void commonStage() {
  if (showBloomAddress) stageSBA();
  if (FacetAct) {
    switch (goFacet) {
    case 0:
      //      if (setupBloomFacet) f_bloom.setupFO();

      f_bloom.drawFO();
      break;
    case 13:
      if (foEDCsetup) {
        f_edc.updateFO();
      }
      break;
    case 14:
      drawFireWorksFF();      
      break;
    case 15:
      f_flower.updateFO();
      break;
    case 100:  //align facet
  f_align.drawFO();
      break;
    default:
      break;
    }
  }
  OverlayStage();
}
void OverlayStage() {
  ChymeraStage();
}

void onetimeSet() {
  loadFlowerNames();
  //  bloom=loadStrings(bloomSrc);
  makeBloom();
  loadCherish();
  //  cherish=loadStrings(cherishSrc);
  envSetup();
  background(0);
}

void commonSetup() {
  oscP5 = new OscP5(this, "239.0.0.1", 7777);
  oscP5x = new OscP5(this, 8000);
  Conductor = new NetAddress("10.0.0.151", 9000);
  f_bloom=new F_Bloom();
  f_align=new F_Align();
  textFont(createFont("sans-serif", 27));

  //! initialize the controller state banks
  echoAction(0, 0, 0);  
  for (int i : flowers) {
    flowers[i] = 0;
  }
  for (int i : int(ranges)) {
    ranges[i] = 0.5;
  }
  for (int i : facets) {
    facets[i] = 0;
  }
}

void facetClear() {
  knownFacet=16;
  for (int i : facets) {
    facets[i] = 0;
  }
}
